package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.VolunteerTeamMember;
import com.tju.ocean_ecology_website.dao.VolunteerTeamMemberDao;
import com.tju.ocean_ecology_website.service.VolunteerTeamMemberService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 志愿者队伍成员表(VolunteerTeamMember)表服务实现类
 */
@Service("volunteerTeamMemberService")
public class VolunteerTeamMemberServiceImpl implements VolunteerTeamMemberService {
    @Resource
    private VolunteerTeamMemberDao volunteerTeamMemberDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public VolunteerTeamMember queryById(Long id) {
        return this.volunteerTeamMemberDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param volunteerTeamMember 筛选条件
     * @param pageRequest         分页对象
     * @return 查询结果
     */
    @Override
    public Page<VolunteerTeamMember> queryByPage(VolunteerTeamMember volunteerTeamMember, PageRequest pageRequest) {
        long total = this.volunteerTeamMemberDao.count(volunteerTeamMember);
        return new PageImpl<>(this.volunteerTeamMemberDao.queryAllByLimit(volunteerTeamMember, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param volunteerTeamMember 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerTeamMember insert(VolunteerTeamMember volunteerTeamMember) {
        Date now = new Date();
        volunteerTeamMember.setCreateTime(now);
        volunteerTeamMember.setUpdateTime(now);
        this.volunteerTeamMemberDao.insert(volunteerTeamMember);
        return volunteerTeamMember;
    }

    /**
     * 修改数据
     *
     * @param volunteerTeamMember 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerTeamMember update(VolunteerTeamMember volunteerTeamMember) {
        volunteerTeamMember.setUpdateTime(new Date());
        this.volunteerTeamMemberDao.update(volunteerTeamMember);
        return this.queryById(volunteerTeamMember.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.volunteerTeamMemberDao.deleteById(id) > 0;
    }

    /**
     * 查询所有数据
     *
     * @param volunteerTeamMember 筛选条件
     * @return 对象列表
     */
    @Override
    public List<VolunteerTeamMember> queryAll(VolunteerTeamMember volunteerTeamMember) {
        return this.volunteerTeamMemberDao.queryAll(volunteerTeamMember);
    }
}
